package com.lmq.innerclasses;// innerclasses/TestParcel.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

class Parcel4 {
  private class PContents implements Contents {
    private int i = 11;
    @Override public int value() { return i; }
  }
  protected final class
  PDestination implements Destination {
    private String label;
    private PDestination(String whereTo) {
      label = whereTo;
    }
    @Override
    public String readLabel() { return label; }
  }
  public Destination destination(String s) {
    return new PDestination(s);
  }
  public Contents contents() {
    return new PContents();
  }
}

public class TestParcel {
  public static void main(String[] args) {
    Parcel4 p = new Parcel4();
    Contents c = p.contents();
    Destination d = p.destination("Tasmania");
//    d.readLabel();
    System.out.println(d.readLabel());
    // Illegal -- can't access private class:
    //- Parcel4.PContents pc = p.new PContents();
    /**
     * In parcel4, the inner class PContents is private , so nothing but Parcel4 can
     * access it.normal ( non-inner) classes cannot be made private or protected; they can
     * only be give public or package access.
     * PDestination is protected, so it can only be accessed by
     * Parcel4, classes in the same package(because protected also gives package access), and
     * the inheritors of Parcel4.This means the client programmer has restricted knowledge and
     * access to these members.In fact, you can't even downcast to a private inner class (or a
     * protected inner class unless you're an inheritor),because you can't access the name,
     * as you see in class TestParcel.
     * private inner classes provide a way for class designer to completely prevent
     * any type-coding dependencies and to completely hide details about
     * implementation.In addition, extension of an interface is useless from the client programmer's perspective because
     * the client programmer cannot access any additional methods that aren't part of the public interface.This also provides an opportunity for
     * java compiler to generate more efficient code.
     */

    /**
     * Inner classes in Methods and Scopes
     * What you've seen so far encompasses that the typical use for inner
     * classes. In genenral, the syntax for inner classes covers several more obscure techniques.
     * Inner classes can be created within a method or even an arbitrary scope.There are
     * two reasons for dong this:
     * 1 As shown previously, you're implementing an interface of some kind so you
     * can create an return a reference.
     * 2 You're solving a complicated problem and you create a class to aid in your solution,
     * but you don't want it publicly available.
     * In the following examples, the previous code is modified to use:
     * 1 A class defined within a method.
     * 2 A class defined within a scope inside a method.
     * 3 An anonymous class implementing an interface.
     * An anonymous class extending a class that has a non-default constructor.
     * 5 an anonymous class that performs field initialization.
     * 6 An annoymous class that performs construction suing instance initialization
     * (annonymous inner classes cannot have constructors)
     */
    /**
     * The first example shows the creation of an entire class within the scope of a method (instead of
     * the scope of another class).This is called a local inner class:
     *
     */
  }
}
